<?php
class Element{
	
	protected $elements;
	
	private $authorizedElements;
	
	private $classname;
	
	public function __construct($authorizedElements  = null){
		$this->elements = array();
		$this->authorizedElements = $authorizedElements;
		$this->classname = '';
	}
	
	public function add_classname($classname){
		if($this->classname == '') $this->classname = $classname;
		else $this->classname .= ' ' . $classname;
	}
	
	public function get_classname(){
		return $this->classname;
	}
	
	public function get_elements(){
		return $this->elements;
	}
	
	public function get_authorizedElements(){
		return $this->authorizedElements;
	}
	
	public function is_authorizedElements($element){
		if($this->authorizedElements == null) return true;
		return in_array(get_class($element),$this->authorizedElements);
	}
	
	public function add_element($element){
		$this->elements[] = $element;
	}
	
	public function add_elements($elements){
		$this->elements = array_merge($this->elements, $elements);
	}
	
	public function separate_links($url){
		$links = array();
		$i = 0;
		for($i = 0; $i < sizeof($this->elements); $i++){
			if(get_class($this->elements[$i]) == 'Link' && !$this->elements[$i]->is_ancre($url)){
				$links[] = $this->elements[$i];
				$this->elements[$i] = new Text($this->elements[$i]->get_text());
			}else{
				if($this->elements[$i]->classname != 'liens'){
					$links = array_merge($links,$this->elements[$i]->separate_links($url));
				}
			}
		}
		return $links;
	}
	
	public function get_text(){
		$text = '';
		$i = 0;
		for($i = 0; $i < sizeof($this->elements); $i++){
			$text .= $this->elements[$i]->get_text();
		}
		return $text;
	}
	
	public function __toString(){
        $text = '';
		foreach($this->elements as $element){
			$text.=(string)$element;
		}
		return $text;
    }

}
?>